package com.haizhi.crm.search.manager.impl;

import com.alibaba.fastjson.JSON;
import com.haizhi.crm.common.constant.SearchStatus;
import com.haizhi.crm.common.exception.UnexpectedStatusException;
import com.haizhi.crm.common.rest.RestService;
import com.haizhi.crm.search.manager.SearchManager;
import com.haizhi.crm.search.model.SearchReq;
import com.haizhi.crm.search.model.SearchResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * Created by chengmo on 2018/5/16.
 */
@Service
public class SearchManagerImpl implements SearchManager {

    @Value("${search.manager.url.graph-search-es:}" )
    private String restUrl;
    @Autowired
    private RestService restService;

    @Override
    public SearchResp search(SearchReq request) {
        try {
            request.setDomain(request.getGraph());
            return restService.doPost(restUrl, request, SearchResp.class);
        } catch (Exception e) {
            throw new UnexpectedStatusException(SearchStatus.CALL_REST_ERROR, e,
                    JSON.toJSONString(request, true));
        }
    }
}
